<?php
ini_set('memory_limit', '-1');
set_time_limit (0);
class ClsHome extends ClsNaanalController
{
   /**
         * CONSTRUCT 
         * @package naanal
         */        
     public function __construct()
     {
        parent::__construct();
     } 
    /**
    * getRedirect
    * @package naanal
    * @return string
    */
   public function getRedirect()
   {
           return $this->redirect;
   }
   public function download()
   {

       //exit;
       $objBusiness=ClsNaanalBusiness::getInstance("report");
       $objBusiness->init();
       $switch=ClsNaanalRequest::getInstance()->getData("switch");
       $from=ClsNaanalRequest::getInstance()->getData("from");
       $from=trim($from);
       $to=ClsNaanalRequest::getInstance()->getData("to");
       $to=trim($to);
       if(!empty($from) && !empty($to))
       {
           $arrFrom=array();
           preg_match("/(\d*)[-\/.](\d*)[-\/.](\d*)/", $from, $arrFrom);
           $from=  mktime(0, 0, 0, isset($arrFrom[2])?$arrFrom[2]:date("m"), $arrFrom[1], isset($arrFrom[3])?$arrFrom[3]:date("Y"));
           $arrTo=array();
           preg_match("/(\d*)[-\/.](\d*)[-\/.](\d*)/", $to, $arrTo);
           $to=mktime(0, 0, 0, isset($arrTo[2])?$arrTo[2]:date("m"), $arrTo[1], isset($arrTo[3])?$arrTo[3]:date("Y"));
           $objBusiness->setRange($from,$to);
       }
       else
       {
            switch($switch)
            {
                case "Today":
                {
                    $start=  mktime(0, 0, 0, date("m"), date("d"), date("Y"));
                    $end=  mktime(23, 59, 59, date("m"), date("d"), date("Y"));
                    $objBusiness->setRange($start,$end);
                    break;
                }
                case "Current Week":
                {
                    $week_num=date("w",time());
                    $first_day=0-$week_num;
                    $start=strtotime( $first_day.' days' );

                    $end_day=$first_day+7;
                    $end = strtotime($end_day.' days');
                    $objBusiness->setRange($start,$end);
                    break;
                }
                case "Current Month":
                {
                    $start=  mktime(0, 0, 0, date("m"), 1, date("Y"));
                    $num_days=date("t",time());
                    $end=  mktime(23, 59, 59, date("m"), $num_days, date("Y"));
                    $objBusiness->setRange($start,$end);
                    break;
                }
                case "Current Year":
                {
                    $start=  mktime(0, 0, 0, 1, 1, date("Y"));
                    $num_days=date("t",time());
                    $end=  mktime(23, 59, 59, 12, 31, date("Y"));
                    $objBusiness->setRange($start,$end);
                    break;
                }
                case "Range":
                {
                    die("Please fill the From Date and To Date");



                }
            }
       }
       $objPHPExcel=ClsNaanalLibrary::getInstance("phpexcel");
       $objPHPExcel = new PHPExcel();
       $ret=$objBusiness->getEnquiryReport();//trace($ret);
       if($ret)
       {
             $objSheet=$objPHPExcel->setActiveSheetIndex(0);
             $objSheet->setTitle("Leads", FALSE);
             $tmp="";
             foreach($ret as $indCell=>$cell)
             {
                 if($cell["xUpper"]>26)
                 {
                     $alphaNum=$cell["xUpper"] % 26;
                     $alphacount=($cell["xUpper"] - $alphaNum) / 26;
                     $firstAlpha=chr($alphacount+64);
                     $charUpper=$firstAlpha.chr($alphaNum + 65);
                     $charLower=chr($cell["xLower"] + 65);
                 }
                 else
                 {
                     $charUpper=chr($cell["xUpper"] + 64);
                     $charLower=chr($cell["xLower"] + 64);
                 }
                 $rowNum=$cell["yUpper"];
                 $tmp=$tmp."{$charUpper}{$rowNum}, ";
                 $asciidata=preg_replace('/[^(\x20-\x7F)]*/','', $cell["data"]);
                 $objSheet->setCellValue("{$charUpper}{$rowNum}", $asciidata);
             }
        }
        /*$ret=$objBusiness->getEnquiryReport(true);
        if($ret)
        {
            $objPHPExcel->createSheet(1);
             $objSheet=$objPHPExcel->setActiveSheetIndex(1);
             $objSheet->setTitle("Registration", FALSE);
             $tmp="";
             foreach($ret as $cell)
             {
                 if($cell["xUpper"]>26)
                 {
                     $alphaNum=$cell["xUpper"] % 26;
                     $charUpper="A".chr($alphaNum + 64);
                     $charLower=chr($cell["xLower"] + 64);
                 }
                 else
                 {
                     $charUpper=chr($cell["xUpper"] + 64);
                     $charLower=chr($cell["xLower"] + 64);
                 }
                 $rowNum=$cell["yUpper"];
                 $tmp=$tmp."{$charUpper}{$rowNum}, ";
                 $cell["data"]=preg_replace("/[^\w.-]/","",$cell["data"]);
                 $objSheet->setCellValue("{$charUpper}{$rowNum}", $cell["data"]);
             }
        }*/
             $saveAs=dirname(__FILE__)."/report.xls";
             unlink($saveAs);
             if(file_exists($saveAs))
             {
                 die("Unable to remove old report file. Please contact your administrator");
             }
             else
             {
                $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
                $objWriter->save($saveAs);
             }
//             ClsNaanalDownload::getInstance()->download($saveAs);
             /*$basename=  basename($saveAs);
             header('Content-Type: application/vnd.ms-excel');
             header('Content-Disposition: attachment; filename='.$basename);
             header('Content-Length: ' . filesize($saveAs));
             readfile($saveAs);
             exit;*/
       /*}
       else
       {
           die("Data not exist");
       }*/
   }
}
?>
